package cn.edu.swu.lzr.Safe;

import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.io.Writer;

public class Login extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException{
        String username = request.getParameter("user");
        String password = request.getParameter("pass");
        String code = request.getParameter("code");
        HttpSession session = request.getSession(true);

        if (code == null || !code.equals(session.getAttribute(Codeservlet.Code)))
        {
            response.sendRedirect("./index.html");
            return;
        }


        if(username != null && username.equals("lzrnb")) {
            System.out.println("用户名正确");
            if (password != null && password.equals("12345")) {
                System.out.println("密码正确");
                session.setAttribute(LogFilter.StatusFilter, StatusFilter.LOGIN_SUCCESS);
                response.sendRedirect("./main");
                return;
            }
        }
        try(Writer writer = response.getWriter()) {
            response.setHeader("Refresh", "5;url=./index.html");
            writer.write("<center><h1 style='color:red'>用户名密码不正确，需要重新登陆，5秒后自动跳转！</h1></center>");
        }
        }

    }

